Page({
  data: {
    relname: "",
    card: "",
    user_id: "",
    cardimg:"",
    cardfimg:"",
    vcardimg:"",
    vcardfimg:"",
    flag:true,
    condition:false,
    conditions:false
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({ user_id: options.user_id });
    var that = this;
    wx.request({
      url: 'https://www.sswan666.top/index/Car/index',
      data: { 'user_id': options.user_id },
      method: 'POST',
      success: function (res) {
        if (res.data.status == '1') {
          //console.log(res.data.info);
          that.setData({
            relname: res.data.info.relname,
            card: res.data.info.card,
            cardimg: res.data.info.cardimg,
            cardfimg: res.data.info.cardfimg,
            condition: true,
            conditions: true
          });
        }
      }
    })
  },

  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          cardimg: res.tempFilePaths,
          condition:true
        });
        var tempFilePaths = res.tempFilePaths;
        upload(that, tempFilePaths,"vcardimg");
      }
    })
  },

  mmp: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          cardfimg: res.tempFilePaths,
          conditions: true
        });
        var tempFilePaths = res.tempFilePaths;
        upload(that, tempFilePaths, "vcardfimg");
      }
    })
  },

  formSubmit: function (e) {
    //console.log('form发生了submit事件，携带数据为：', e.detail.value)
    wx.request({
      url: 'https://www.sswan666.top/index/Car/addUser',
      data: e.detail.value,
      method:"POST",
      success:function(res){
        console.log(res);
      }
    })
  },

  formReset: function () {
    //console.log('form发生了reset事件')
  }

});

function upload(page, path,imgName) {
  var cardname = imgName;
  wx.showToast({
    icon: "loading",
    title: "正在上传",
    duration:3000
  })
    wx.uploadFile({
      url: "https://www.sswan666.top/index/car/uploadImg",
      filePath: path[0],
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      /*formData: {
        //和服务器约定的token, 一般也可以放在header中
        'session_token': wx.getStorageSync('session_token')
      },*/
      success: function (res) {
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }
        var data = JSON.parse(res.data);
        //console.log(data);
        page.setData({  //上传成功修改显示头像
          [cardname]:data.info.url,
          flag:false
        })
      },
      fail: function (e) {
        //console.log('false');
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast
        
      }
    })
}
